package com.hbyj.struts.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.batik.dom.util.IntTable;
import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.CDBInfo;
import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.Functions;
import com.hbyj.hbm.vo.Jbqk01;
import com.hbyj.hbm.vo.Jbqk01temp;
import com.hbyj.hbm.vo.YjczYjzy01;
import com.hbyj.hbm.vo.YjczYjzy02;
import com.hbyj.hbm.vo.YjczYpxx01;
import com.hbyj.object.ZTreeNode;
import com.hbyj.util.ChangeTool;
import com.hbyj.util.Constants;
import com.hbyj.util.JsonUtil;
import com.hbyj.util.PageClass;
import com.opensymphony.xwork2.ActionSupport;

public class BaseInfoStateAction extends ActionSupport{
	private int total;
	private List rows = new ArrayList();
	private List jcjgRows = new ArrayList();
	private List yjzyRows = new ArrayList();
	private String jsonResult;
	private String stateType;////统计类型 以便于加载不同swf
	private String[] tblArr = {"JcdcJcdc00","JcdcJcdc01","JcdcJcdc02","JcdcJcdc03","JcdcJcdc04","JcdcJcdc06","JcdcJcdc07"};
	//表名与jbqk01关联的字段名 数组
	private String[] idArr = {"jcdc00001","jcdc01001","jcdc02001","jcdc03001","jcdc04001","jcdc06001","jcdc07001"};
	//威胁房屋数组
	private String[] wxfw  ={"jcdc00102","jcdc01113","jcdc02118","jcdc03172","jcdc04157","jcdc06145","jcdc07050"};
	//威胁人口数组
	private String[] wxrk = {"jcdc00108","jcdc01118","jcdc02124","jcdc03059","jcdc04162","jcdc06147","jcdc07060"};
	//威胁户数 数组
	private String[] wxhk = {"jcdc00130","jcdc01117","jcdc02150","jcdc03170","jcdc04190","jcdc06170","jcdc07070"};
	//威胁资产数组
	private String[] wxzc = {"jcdc00109","jcdc01119","jcdc02125","jcdc03060","jcdc04163","jcdc06148","jcdc07080"};
	
	public List getJcyjRows() {
		return jcjgRows;
	}
	public void setJcyjRows(List jcjgRows) {
		this.jcjgRows = jcjgRows;
	}
	public List getYjzyRows() {
		return yjzyRows;
	}
	public void setYjzyRows(List yjzyRows) {
		this.yjzyRows = yjzyRows;
	}
	public String getJsonResult() {
		return jsonResult;
	}
	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public List getRows() {
		return rows;
	}
	public void setRows(List rows) {
		this.rows = rows;
	}
	
	
	public String getStateType() {
		return stateType;
	}
	public void setStateType(String stateType) {
		this.stateType = stateType;
	}
	/**
	 * author cuiyi
	 * time 2013-1-8
	 * disc 年度应急调查次数统计
	 * @return
	 */
	public String stateYjdcCountByYears(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String region = request.getParameter("region");
		String sYear_str = request.getParameter("sYear");
		String eYear_str = request.getParameter("eYear");
		stateType =Constants.ScrollColumn2D;
		region = ChangeTool.ChangeArea(region);
		int sYear = Integer.parseInt(sYear_str);
		int eYear = Integer.parseInt(eYear_str);
		int currentYear = sYear;
		jsonResult = convertChartXmlForStateYjdcCountByYears(region,sYear,eYear);
		
		
		
		return SUCCESS;
	}
	/**
	 * author cuiyi
	 * time 2013-1-8
	 * disc 年度应急调查点各规模灾害点所占百分比
	 * @return
	 */
	public String stateYjdcCountByOneYear(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String region = request.getParameter("region");
		String year = request.getParameter("year");
		stateType = Constants.Pie3D;//统计类型 以便于加载不同swf
		
		region = ChangeTool.ChangeArea(region);
		String hql = "select yjdc01240,count(*) from YjdcYjdc01 where  yjdc01000 like '%"+region+"%' and yjdc01270>='"+year+"-1-1 00:00:00' and yjdc01270<='"+year+"-12-31 23:59:59' group by yjdc01240";
		List list = DbTools.findByHql(hql);
		jsonResult = coverntChartXmlForStateYjdcCountByOneYear(year,list);
		return SUCCESS;
	}
	/**
	 * author cuiyi
	 * time 2013-1-8
	 * disc 横轴为业务类型（5种），纵轴为个数
	 * @return
	 */
	public String mutilpeStateBaseInfoGroupByOper(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String region = request.getParameter("region");
		region = ChangeTool.ChangeArea(region);
		stateType = Constants.Column2D;//统计类型 以便于加载不同swf
		String hql = "select id.jbqk0102,count(id.jbqk0102) from Jbqk01 as jbqk where jbqk.id.jbqk0100 like '%"+region+"%' group by id.jbqk0102 order by id.jbqk0102 ";
		List list = DbTools.findByHql(hql);
		jsonResult = convertChartXmlForMutilpeStateBaseInfoGroupByOper(list);
		return SUCCESS;
	}
	
	/**
	 * author cuiyi
	 * time 2013-1-8
	 * disc 业务数据综合统计  威胁对象：威胁人数、威胁户数、威胁房屋数（栋）
	 * @return
	 */
	public String mutilpeStateBaseInfoGroupByTheat(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String region = request.getParameter("region");
		region = ChangeTool.ChangeArea(region);
		stateType = Constants.Column2D;//统计类型 以便于加载不同swf
		jsonResult = convertMutilpeStateBaseInfoGroupByTheat(region);
		return SUCCESS;
	}
	/**
	 * author cuiyi
	 * time 2013-1-15
	 * disc 影响区统计
	 * @return
	 */
	public String affectAreaStatTheat(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String graphParam = request.getParameter("graphParam");//图形范围参数  圆  x,y,r  矩形 xMin,yMin,xMax,yMax
		rows = convertXmlForAffectAreaStatTheat(graphParam);
		return SUCCESS;
	}
	/**
	 * author cuiyi
	 * time 2013-1-8
	 * disc 年度应急调查点各规模灾害点所占比例   封装XML
	 * @param list
	 * @return
	 */
	private String coverntChartXmlForStateYjdcCountByOneYear(String year,List list){
		String res = "";
		String colorStr = "";
		for(int i =0 ;i<list.size();i++){
			Object[] obj = (Object[])list.get(i);
			
			int code = Integer.parseInt(obj[0].toString());
			res +="<set label='"+ChangeTool.getZhGMByCode(code)+"' value='"+obj[1]+"'/>";
			colorStr +=ChangeTool.getColorByCode(code)+",";
		}
		colorStr = colorStr.length()>0?colorStr.substring(0,colorStr.length()-1):"";
		res +="</chart>";
		res = "<chart palette='4'  interactiveLegend=\"1\" showLegend=\"0\"  paletteColors='"+colorStr+"'  caption='"+year+"年度应急调查点各规模灾害点所占比例' decimals='0' enableSmartLabels='1' enableRotation='0' bgColor='99CCFF,FFFFFF' bgAlpha='40,100' bgRatio='0,100' bgAngle='360' showBorder='1' startingAngle='70'>"+
				res ;
		return res ;
	}
	/**
	 * author cuiyi
	 * time 2013-1-9
	 * disc 横轴为业务类型（5种），纵轴为个数  封装xml
	 * @param list
	 * @return
	 */
	private String convertChartXmlForMutilpeStateBaseInfoGroupByOper(List list){
		String res = "<chart palette='2'  caption='区域业务点按规模分类统计' xAxisName='业务点类别' yAxisName='灾害点个数' showValues='0' decimals='0' formatNumberScale='0' useRoundEdges='1'>";
		
		for(int i =0 ;i<list.size();i++){
			Object[] obj = (Object[])list.get(i);
			res +="<set label='"+ChangeTool.getJbqk01TypeByCode(Integer.parseInt(obj[0].toString()))+"' value='"+obj[1]+"'/>";
		}
		res +="</chart>";
		return res ;
	}
	
	/**
	 * author cuiyi
	 * time 2013-1-9
	 * disc 这个代码太烂了。。。不知道怎么优化。cy
	 * @param region 行政区代码
	 * @param sYear 起始年份
	 * @param eYear 终止年份
	 * @return
	 */
	private String convertChartXmlForStateYjdcCountByYears(String region ,int sYear,int eYear){
		String res = "<chart palette='3' legendIconScale=\"5\" showLegend=\"1\"  reverseLegend=\"0\" legendAllowDrag=\"1\" placeValuesInside=\"0\" paletteColors='FF0000,FFAD5B,FFFF00,0000FF' caption='多年度应急调查次数统计' numberprefix='' xaxisName='年度' useRoundEdges='1' showValues='0' legendBorderAlpha='1'>";
		res +="<categories>";
		String hql = "";
		String[][] tempRes = new String[eYear-sYear+1][4];
		String[] dataset = new String[4];
 		int currentYear  = sYear;
		for(int j=0;currentYear<=eYear;currentYear++,j++){
			
			res += "<category label='"+currentYear+"'/>";
			hql = "select yjdc01240,count(*) from YjdcYjdc01 where  yjdc01000 like '%"+region+"%' and yjdc01270 >='"+currentYear+"-1-1 00:00:00' and yjdc01270<='"+currentYear+"-12-31 23:59:59' and yjdc01240 in(1,2,3,4) group by yjdc01240 order by yjdc01240 desc";
			List list = DbTools.findByHql(hql);
			for(int i = 0;i<list.size();i++){
				Object[] obj = (Object[])list.get(i);
				String t = obj[0].toString();
				int secondIndex =Integer.parseInt(t)-1;
				tempRes[j][secondIndex] = obj[1].toString() ;
			}
			
		}
		res +="</categories>";
		
		for(int i = 3;i>=0;i--){
			currentYear  = sYear;
			String color = ChangeTool.getColorByCode(i+1);
			res += "<dataset color=\""+color+"\" seriesname='"+ChangeTool.getZhGMByCode(i+1)+"'>";
			for(int j=0;currentYear<=eYear;currentYear++,j++){
				String val = tempRes[j][i];
				res += val==null?"<set/>":"<set color='"+color+"' value='"+val+"'/>";
			}
			res +="</dataset>";
		}
		res +="</chart>";
		//res ="<chart palette=\"3\" animation=\"1\"  paletteColors=\"FF0000,FFAD5B,FFFF00,0000FF\" caption=\"Daily Sales\" numberprefix=\"$\" xaxisName=\"Day\" useRoundEdges=\"1\" showValues=\"0\" legendBorderAlpha=\"0\">"+
//"<categories><category label=\"1\"/><category label=\"2\"/><category label=\"3\"/><category label=\"4\"/><category label=\"5\"/></categories><dataset seriesname=\"Product A\"><set color=\"0000FF\" value=\"57000\"/><set value=\"58444\"/><set value=\"76332\"/><set value=\"78927\"/><set value=\"81927\"/></dataset><dataset seriesname=\"Product B\"><set value=\"15500\"/><set value=\"16361\"/><set value=\"17212\"/><set value=\"18443\"/><set value=\"15500\"/></dataset><dataset seriesname=\"Product C\"><set value=\"38000\"/><set value=\"39330\"/><set value=\"40746\"/><set value=\"42335\"/><set value=\"38000\"/></dataset><dataset seriesname=\"Product D\"><set value=\"29221\"/><set value=\"30681\"/><set value=\"31665\"/><set value=\"33109\"/><set value=\"34062\"/></dataset></chart>";
		//System.out.println(res);
		return res;
	}
	private String convertMutilpeStateBaseInfoGroupByTheat(String region){
		String res = "<chart palette='3'  caption='区域业务点按威胁对象分类统计' xAxisName='威胁对象类别' yAxisName='数量' showValues='0' decimals='0' formatNumberScale='0' useRoundEdges='1'>";
		
		int[] theatArr = new int[3];  //威胁人口、威胁户口、威胁房屋
		String[] theatArrName = {"威胁人口","威胁户数","威胁房屋"};
		//表名 数组    4个数组顺序一致
		String hql = "";
		for(int i = 0;i<7;i++){
			// 人口
			//hql = "select sum(jcdc00108 ) from JcdcJcdc00";
			hql = "select sum("+wxrk[i]+") from "+tblArr[i]+"  where   "+idArr[i]+" like '"+region+"%' ";
			List list = DbTools.findByHql(hql);
			int num = list.get(0)==null?0:Integer.parseInt(list.get(0).toString());
			theatArr[0] +=num;
			//户数
			hql = "select sum("+wxhk[i]+") from "+tblArr[i]+" where  "+idArr[i]+" like '"+region+"%' ";
			list = DbTools.findByHql(hql);
			num =  list.get(0)==null?0:Integer.parseInt(list.get(0).toString());
			theatArr[1] +=num;
			//房屋
			hql = "select sum("+wxfw[i]+") from "+tblArr[i]+" where  "+idArr[i]+" like '"+region+"%' ";
			list = DbTools.findByHql(hql);
			num = list.get(0)==null?0:Integer.parseInt(list.get(0).toString());
			theatArr[2] +=num;
		}
		for(int i =0 ;i<theatArr.length;i++){
			res +="<set label='"+theatArrName[i]+"' value='"+theatArr[i]+"'/>";
		}
		res +="</chart>";
		return res;
	}
	private List convertXmlForAffectAreaStatTheat(String graphParam){
		String conStr = "";
		String hql = "";
		List resList = new ArrayList();
		List list = new ArrayList();
		String jbqkStr = "distinct jbqk01.id.jbqk0100,jbqk01.jbqk0110,jbqk01.jbqk0120, jbqk01.jbqk0130,jbqk01.jbqk0140,jbqk01.jbqk0150,jbqk01.jbqk0160,jbqk01.jbqk0170,jbqk01.jbqk0180,jbqk01.jbqk0190,jbqk01.id.jbqk0102";
		if(!"".equals(graphParam)){
			String[] graphParamArr = graphParam.split(",");
			if(graphParamArr.length==3){
				conStr +=" and ((jbqk01.jbqk0120-"+graphParamArr[0]+")*(jbqk01.jbqk0120-"+graphParamArr[0]+")+(jbqk01.jbqk0130-"+graphParamArr[1]+")*(jbqk01.jbqk0130-"+graphParamArr[1]+"))< "+graphParamArr[2]+"*"+graphParamArr[2];
			}else{
				conStr +=" and jbqk01.jbqk0120 >"+graphParamArr[0]+" and jbqk01.jbqk0120<"+graphParamArr[2]+" and  jbqk01.jbqk0130>"+graphParamArr[1]+" and  jbqk01.jbqk0130<"+graphParamArr[3];
			}
		}
		//hql =  "select "+jbqkStr+" from Jbqk01 as jbqk01 where  jbqk01.id.jbqk0100 like '42%' "+conStr;
		//rows = DbTools.parseBaleInfoObj(DbTools.findByHql(hql));
		//total = rows.size();
		////统计威胁人口、威胁户数、威胁财产
		for (int i=0;i<7;i++){
			
			hql = "select "+jbqkStr+",tbl."+wxrk[i]+",tbl."+wxhk[i]+",tbl."+wxzc[i]+" from Jbqk01 as jbqk01,"+tblArr[i]+" as tbl where  jbqk01.id.jbqk0100 ="+idArr[i]+conStr;
			list = DbTools.findByHql(hql);
			resList.add(list);
		}
		///监测机构    
		///select distinct jcba01a010,jcba01a020,jcba01a030,JCBA01A100,JCBA01A120 from jbqk01,zyqc_jcba01a where  jcba01a015= substring(jbqk0100,0,7)
		hql = "select distinct jcba01a.jcba01a010,jcba01a.jcba01a020,jcba01a.jcba01a030,jcba01a.jcba01a100,jcba01a.jcba01a120 from Jbqk01 as jbqk01,ZyqcJcba01a as jcba01a where  jcba01a.jcba01a015= substring(jbqk01.id.jbqk0100,0,7) " +conStr;
		
		jcjgRows = DbTools.findByHql(hql);///机构名称、机构类别、负责人、联系电话
		////应急资源
		///select distinct YJZY01020,YJZY01070,YJZY01080,YJZY01090,YJZY01030 from jbqk01,YJCZ_YJZY01 where  yjzy01010= substring(jbqk0100,0,7)
		hql = "select distinct yjzy.yjzy01020,yjzy.yjzy01070,yjzy.yjzy01080,yjzy.yjzy01090,yjzy.yjzy01030 from Jbqk01 as jbqk01,YjczYjzy01 as yjzy where  yjzy.yjzy01010= substring(jbqk01.id.jbqk0100,0,7) " +conStr;
		yjzyRows = DbTools.findByHql(hql);////资源所属单位、资源管理负责人、电话,资源使用状态、资源类型
		return resList;
	}
	
}
